package com.example.planetexploder.module.picture;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.planetexploder.R;
import com.example.planetexploder.adapter.MyAdapter_Picture;
import com.example.planetexploder.bean.maindata.BookInfo;
import com.example.planetexploder.bean.maindata.ImgInfo;
import com.example.planetexploder.bean.maindata.ItemInfo;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

public class View_ImgDisplay extends AppCompatActivity implements AdapterView.OnItemLongClickListener {

    private BookInfo bookInfo;
    private ItemInfo itemInfo;
    private ImgInfo imgInfo;
    private Presenter_Display presenterDisplay;

    private GridView mGridView;
    private MyAdapter_Picture mAdapter;
    private boolean isShowDelete = false;

    private ArrayList<Bitmap> mListBitmap;
    private ArrayList<ImgInfo> mListImgInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpicture);

        //取得启动该Activity的Intent对象
        Intent intent = getIntent();
        //取出Intent中附加的数据
        itemInfo = (ItemInfo) intent.getSerializableExtra("ItemInfo");
        bookInfo = (BookInfo) intent.getSerializableExtra("BookInfo");

        presenterDisplay = new Presenter_Display(getApplicationContext(), bookInfo, itemInfo);
        if (itemInfo.itemId == null)
            itemInfo = presenterDisplay.createNewItem();

        mListBitmap = presenterDisplay.getImgGroupSorce();
        mListImgInfo = presenterDisplay.getImgGroupInfo();



        mGridView = findViewById(R.id.gridViewDisplay);
        mGridView.setOnItemLongClickListener(this);//长按事件监听
        mAdapter = new MyAdapter_Picture(View_ImgDisplay.this, mListBitmap);
        mGridView.setAdapter(mAdapter);

        //点击加号
        FloatingActionButton btn_cam = findViewById(R.id.buttonCamera);
        btn_cam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgInfo = new ImgInfo();
                Intent intent = new Intent(View_ImgDisplay.this, View_ImgProcess.class);
                intent.putExtra("ItemInfo",itemInfo);//传递Book类和Item类
                intent.putExtra("BookInfo",bookInfo);
                intent.putExtra("ImgInfo",imgInfo);
                startActivity(intent);
            }
        });

    }

    /**
     * 功能：实现对显示页面中的长按删除功能，长按获取位置position，删除信息
     * 修改日期：2019.10.19
     */
    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
        if (isShowDelete) {
            isShowDelete = false;
        } else {
            isShowDelete = true;
            mAdapter.setIsShowDelete(isShowDelete);
            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    deleteGridViewItems(position);//删除选中项
                    presenterDisplay.delete_Img(mListImgInfo.get(position).getImgId());
                    refreshGridView();//刷新ArrayList数组并重新绑定容器
                }

            });
        }
        mAdapter.setIsShowDelete(isShowDelete);//setIsShowDelete()方法用于传递isShowDelete值
        return true;
    }

    /**
     * 功能：删除选中项方法
     * 修改日期：2019.10.19
     */
    private void deleteGridViewItems(int position) {
        ArrayList<Bitmap> newList = new ArrayList<Bitmap>();
        if (isShowDelete) {
            mListBitmap.remove(position);
            isShowDelete = false;
        }
        newList.addAll(mListBitmap);
        mListBitmap.clear();
        mListBitmap.addAll(newList);
    }

    /**
     * 功能：刷新ArrayList数组并重新绑定容器
     * 修改日期：2019.10.21
     */
    public void refreshGridView() {
        mListBitmap = presenterDisplay.getImgGroupSorce();
        mListImgInfo = presenterDisplay.getImgGroupInfo();
        mAdapter = new MyAdapter_Picture(View_ImgDisplay.this, mListBitmap);//重新绑定一次adapter
        mGridView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();//刷新GridView
    }
}
